package com.rent.house.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rent.common.entity.ResponseResult;
import com.rent.house.pojo.entity.Collection;
import com.rent.house.pojo.entity.HouseResource;
import com.rent.house.service.CollectionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 收藏控制器
 */
@RestController
@RequestMapping("/api/collection")
@Api(tags = "收藏管理")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    @PostMapping("/add")
    @ApiOperation("收藏房源")
    public ResponseResult<?> collectHouse(
            @ApiParam("房源ID") @RequestParam Long houseId) {
        boolean result = collectionService.collectHouse(houseId);
        if (result) {
            return ResponseResult.success("收藏成功");
        } else {
            return ResponseResult.fail("收藏失败");
        }
    }

    @DeleteMapping("/{houseId}")
    @ApiOperation("取消收藏")
    public ResponseResult<?> uncollectHouse(
            @ApiParam("房源ID") @PathVariable Long houseId) {
        boolean result = collectionService.uncollectHouse(houseId);
        if (result) {
            return ResponseResult.success("取消收藏成功");
        } else {
            return ResponseResult.fail("取消收藏失败");
        }
    }

    @GetMapping("/check/{houseId}")
    @ApiOperation("检查是否已收藏")
    public ResponseResult<?> isCollected(
            @ApiParam("房源ID") @PathVariable Long houseId) {
        boolean result = collectionService.isCollected(houseId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("isCollected", result);
        return ResponseResult.success(resultMap);
    }

    @GetMapping("/myList")
    @ApiOperation("我的收藏列表")
    public ResponseResult<Page<Collection>> getMyCollections(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNo,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Collection> page = collectionService.getMyCollections(pageNo, pageSize);
        return ResponseResult.success(page);
    }

    @PostMapping("/myHouses")
    @ApiOperation("我收藏的房源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", defaultValue = "1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", dataType = "Integer", paramType = "query")
    })
    public ResponseResult<Page<HouseResource>> getMyCollectionHouses(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<HouseResource> page = collectionService.getMyCollectionHouses(pageNo, pageSize);
        return ResponseResult.success(page);
    }
}
